﻿using System;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EPiServer.Shell;
using OpenWaves.EPiServer.Comments.Content.ContentWrappers;
using OpenWaves.EPiServer.Comments.Controllers;
using OpenWaves.Web;

namespace OpenWaves.EPiServer.Comments.Controls
{
    public class CommentsControl : WebControl
    {
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public PlaceHolder TemplateForAuthenticatedUser { get; set; }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public PlaceHolder TemplateForNotAuthenticatedUser { get; set; }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public PlaceHolder LoadingInProgressTemplate { get; set; }

        public string ContentGuid { get; set; }


        protected override void OnInit(EventArgs e)
        {
            SetDefaultTemplatesIfCustomTemplatesNotProvided();
        }
        

        protected override void OnLoad(EventArgs e)
        {
            DataBind();
        }
        

        protected override void CreateChildControls()
        {
            var contentWrapper = ServiceLocator.Resolve<IContentWrapper>();

            Controls.Add(contentWrapper.WrapContent(HttpContext.Current.User.Identity.IsAuthenticated
                                                        ? TemplateForAuthenticatedUser
                                                        : TemplateForNotAuthenticatedUser));
            Controls.Add(contentWrapper.WrapLoadingInProgressContent(LoadingInProgressTemplate));
        }


        protected override void OnPreRender(EventArgs e)
        {
            RegisterRequiredClientScripts();
        }


        protected virtual void RegisterRequiredClientScripts()
        {
            var declarationScript = String.Format(CultureInfo.InvariantCulture,
                                                  @"var {0} = CreateCommentsAgregatorViewModel('{0}', '{1}', '{2}');",
                                                  this.ClientID,
                                                  ContentGuid,
                                                  GetPathToCommentsController());

            Page.ClientScript.RegisterClientScriptBlock(
                typeof(CommentsControl),
                "declaration",
                declarationScript,
                true);
        }


        protected virtual string GetPathToCommentsController()
        {
            return Paths.ToResource(typeof (CommentsController), "Comments");
        }


        protected virtual void SetDefaultTemplatesIfCustomTemplatesNotProvided()
        {
            var defaultTemplate = GetCommentControlDefaultTemplate();

            if (defaultTemplate == null)
                return;

            if (TemplateForAuthenticatedUser == null)
                TemplateForAuthenticatedUser = defaultTemplate.TemplateForAuthenticatedUser;

            if (TemplateForNotAuthenticatedUser == null)
                TemplateForNotAuthenticatedUser = defaultTemplate.TemplateForNotAuthenticatedUser;

            if (LoadingInProgressTemplate == null)
                LoadingInProgressTemplate = defaultTemplate.LoadingInProgressTemplate;
        }


        protected virtual CommentControlDefaultTemplate GetCommentControlDefaultTemplate()
        {
            return Page.LoadControl(
                    AssemblyResourceVirtualPathProvider.CreateUrl(typeof(CommentControlDefaultTemplate)).ToString()) as
                CommentControlDefaultTemplate;
        }
    }
}
